package com.centaline.androidsalesblog.adapter;

import android.content.Context;
import android.view.View;

import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.adapter.viewholder.SearchHistoryVH;
import com.centaline.androidsalesblog.iml.RecyclerViewItemOnClickListener;
import com.centaline.androidsalesblog.model.databaseobject.SearchDo;
import com.centanet.framework.base.BaseAdapter1;

import java.util.ArrayList;

/**
 * 搜索历史记录
 * Created by guilin on 16/5/4.
 */
public class SearchHistoryAdapter extends BaseAdapter1<SearchHistoryVH> {

    private final ArrayList<SearchDo> list;
    private final RecyclerViewItemOnClickListener<SearchDo> itemOnClickListener;

    public SearchHistoryAdapter(Context context, ArrayList<SearchDo> list,
                                RecyclerViewItemOnClickListener<SearchDo> itemOnClickListener) {
        super(context);
        this.list = list;
        this.itemOnClickListener = itemOnClickListener;
    }

    @Override
    protected int inflateByLayoutId() {
        return R.layout.item_search_history;
    }

    @Override
    protected SearchHistoryVH viewHolder(View view) {
        return new SearchHistoryVH(view);
    }

    @Override
    public void onBindViewHolder(final SearchHistoryVH holder, int position) {
        if (list.size() == position) {//删除
            holder.fl_history.setVisibility(View.GONE);
            holder.atv_clear.setVisibility(View.VISIBLE);
        } else {
            holder.atv_title.setText(list.get(position).getTitle());
            holder.fl_history.setVisibility(View.VISIBLE);
            holder.atv_clear.setVisibility(View.GONE);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemOnClickListener.onItemOnClick(list.size() == holder.getAdapterPosition() ?
                        null : list.get(holder.getAdapterPosition()), 0);
            }
        });
    }

    @Override
    public int getItemCount() {
        int size = list.size();
        return size == 0 ? 0 : size + 1;
    }
}
